﻿@using Newtouch.Herp.Infrastructure;
@{
    ViewBag.Title = "库存结转";
    Layout = "~/Views/Shared/_Index.cshtml";
    var reportUrl = Newtouch.Core.Common.Utils.ConfigurationHelper.GetAppConfigValue("ReportUrl");
    var reportSystemCode = Newtouch.Core.Common.Utils.ConfigurationHelper.GetAppConfigValue("ReportSystemCode");

}
<style>
    #btn_search {
        float: left;
        margin-left: 25px;
        margin-right: 10px;
        width: 45px;
        font-weight: bolder;
    }

    #btn_print {
        width: 45px;
        background-color: #fff;
        color: #00a9ea;
        font-weight: bolder;
        float: left;
    }
</style>
<form id="form1">
    <div class="panel panel-default">
        <div class="panel-heading">
            结转
        </div>
        <div>
            <table class="form">
                <tr>
                    <th class="formTitle">历史结转：</th>
                    <td class="formValue">
                        <select id="lsjz" class="form-control"></select>
                    </td>
                    <th class="formTitle">输入码：</th>
                    <td class="formValue">
                        <input id="txtSRM" type="text" class="form-control" placeholder="物资名称/拼音" />
                    </td>
                    <td class="formValue" colspan="3">
                        <input type="button" id="btn_search" class="btn btn-primary" value="查询" style="margin-left:10px;" />
                        <input type="button" id="btn_print" class="btn btn-primary" value="打印" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="gridPanel">
        <table id="gridDrugsList"></table>
        <div id="gridPager" rel="form1"></div>
    </div>
    @Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new[] { 8 },
    F8Text = "结转当前"
})
</form>
<script>
    $(function () {
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function () {
            window.initLayout("MyTabGrid");
        });
        GetlsjzSel();//获取历史结转时间
        gridDrugsList();//物资信息
    });

    //回车事件绑定
    $(document).keyup(function (event) {
        if (event.keyCode === 13) {
            $("#btn_search").trigger("click");
        }
    });

    //获取历史结转时间
    function GetlsjzSel() {
        $("#lsjz").html("");
        $("#lsjz").bindSelect({
            url: "/StorageManage/StockCarryOver/GetlsjzDateTime",
            id: "value",
            text: "text"
        });
    }

    //执行结转当前
    function newtouch_event_f8() {
        //获取最后一次结转时间
        $.najax({
            type: "Get",
            url: "/StorageManage/StockCarryOver/QueryLastCarryTime?r=" + Math.random(),
            dataType: "json",
            success: function (ajaxresp) {
                if (ajaxresp != null) {
                    if (ajaxresp.state === "success") {
                        var alertMmsg = ajaxresp.data == null || ajaxresp.data === "" ? "您确定结转吗？" : "最近一次结转时间为：" + ajaxresp.data + " ,您确定结转吗？";
                        $.modalConfirm(alertMmsg,
                            function (flag) {
                                if (flag) {
                                    $.najax({
                                        url: "/StorageManage/StockCarryOver/CarryOverProduct",
                                        type: "POST",
                                        dataType: "json",
                                        success: function (ajaxresp) {
                                            if (ajaxresp.state === "success") {
                                                $.modalMsg("结转成功", 'success', 1500);
                                                GetlsjzSel();//刷新结转时间
                                                $("#btn_search").trigger("click");
                                            }
                                            else {
                                                var msg = ajaxresp.message != null ? ajaxresp.message : "结转失败";
                                                $.modalMsg(msg, "error", 1500);
                                            }
                                        }
                                    });

                                } else {
                                    return false;
                                }
                            }, true);
                    } else {
                        $.modalAlert(ajaxresp.message, "error");
                    }
                } else {
                    $.modalAlert("取上一次结转时间失败", "error");
                }
            }
        });
    }

    //物资信息
    function gridDrugsList() {
        var captionCon = "合计&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id='yeCon'>当前页 零售总额：元</span>";
        var $gridDrugsList = $("#gridDrugsList");
        $gridDrugsList.dataGrid({
            postData: getSearchPostData(),
            height: $(window).height() - document.getElementById('gridDrugsList').offsetTop - 150,
            url: "/StorageManage/StockCarryOver/SelectCarryOverDetail",
            caption: captionCon,
            colModel: [
                { label: '物资名称', name: 'wzmc', width: 120, align: 'left' },
                { label: '规格', name: 'gg', width: 80, align: 'left' },
                { label: '结转数量', name: 'jzsl', width: 60, align: 'left' },
                { label: '批次', name: 'pc', width: 120, align: 'left' },
                { label: '批号', name: 'ph', width: 110, align: 'left' },
                {
                    label: '零售单价', name: 'lsj', width: 80, align: 'right',
                    formatter: function (val) {
                        return val.toFixed(4);
                    }
                },
                { label: '单位', name: 'bmdwmc', width: 50, align: 'center' },
                {
                    label: '零售总额', name: 'lsze', width: 100, align: 'right',
                    formatter: function (val) {
                        return val.toFixed(2);
                    }
                },
                { label: '生产厂家', name: 'supplierName', width: 120, align: 'left' },
                { label: '创建时间', name: 'CreateTime', width: 50, align: 'left', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i' } },
                { label: '结转库存数量', name: 'kcsl', hidden: true },
                { label: 'productId', name: 'productId', hidden: true }
            ],
            multiselect: false,
            pager: "#gridPager",
            sortname: 'wzmc',
            viewrecords: true,
            loadComplete: function (data) {
                ComputePjzeAndLsze();//计算标题栏中的批价总额和零售总额
            }
        });
        //查询
        $("#btn_search").click(function () {
            $gridDrugsList.jqGrid('setGridParam', {
                postData: getSearchPostData()
            }).trigger('reloadGrid');
        });
        //打印
        $("#btn_print").click(function () {
            window.open('@reportUrl' + "?tempCode=1270" + "&systemCode=" + '@reportSystemCode' + "&hospitalCode=" + '@ViewBag.OrganizeId' + "&OrganizeId=@ViewBag.OrganizeId&warehouseId=@Constants.CurrentKf.currentKfId" +
               "&jzsj=" + ($("#lsjz option:selected").val()) +
               "&keyWord=" + ($('#txtSRM').val() === "" ? "" : $('#txtSRM').val()));
        });
    }

    //查询条件
    function getSearchPostData() {
        var jzsj = $("#lsjz").find("option:selected").text();
        var keyWork = $("#txtSRM").val();
        return { jzsj: jzsj, keyWork: keyWork };
    };

    //计算标题栏中的批价总额和零售总额
    function ComputePjzeAndLsze() {
        var titleLsze = 0;
        var count = $("#gridDrugsList").getGridParam('records');
        if (count > 0) {
            var rowIds = $("#gridDrugsList").getDataIDs();
            for (var i = 0; i < rowIds.length; i++) {
                var getRowLsze = $("#gridDrugsList").getRowData(rowIds[i]).lsze;
                titleLsze += parseFloat(getRowLsze);
            }
            titleLsze = window.roundingBy4she6ru5chengshuang(titleLsze, 2);
            $("#yeCon").text("当前页 零售总额：" + titleLsze + "元");
        }
    }
</script>